var prefix_path="http://123.56.219.116:8080/virtualcat/manage/cat/";

var $req_data;

$(function () {
    $req_data="pageNum=1";
    ajax_catList();
    $("#pagination li").click(function () {
        var index_li=$(this).index();
        var flag=true;
        var $curPage=$("#pagination li:eq(2)").text();
        if ($("#pagination li:last").text()== $curPage){
            flag=false;
        }
        if (index_li==0){
            $req_data ="pageNum=1";
        }else if (index_li==1) {
            if ($curPage==1){
                alert("已经是第一页");
            } else{
                $req_data ="pageNum="+(parseInt($curPage)-1);
            }

        }else if (index_li==3) {
            if (flag){
                $req_data ="pageNum="+(parseInt($curPage)+1);
            } else{
                alert("已经是最后一页");
                return;
            }
        }else if (index_li==4) {
            $req_data ="pageNum="+$("#pagination li:last").text();
        }
        ajax_catList();
    });
});

function ajax_catList() {
    $.ajax({
        url:prefix_path+"searchCatList",
        type:"GET",
        dataType:"json",
        data:$req_data,
        success:function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html";
                return;
            }
            console.log(data);
            if (data.data.catlist == null){
                alert("无信息");
                return;
            }
            $("#pagination li:last").html(data.data.catlist.lastPage);
            $("#pagination li:eq(2)>a").html(data.data.catlist.pageNum);
            var preTotal=(parseInt(data.data.catlist.pageNum)-1)*parseInt(data.data.catlist.pageSize);
            var $tbody = $("#catListTable tbody");
            $tbody.html("");
            var $tr_array = "";
            console.log(data.data.catlist.list);
            $.each(data.data.catlist.list,function (index, value) {
                $tr_array += "<tr><td>"+(index+1+preTotal)+"</td><td>"
                    +value.cname+"</td><td><img src='"+value.cimg+"' alt='无图片' width=60px height=60px /></td><td>"
                    +value.nprice+"</td><td width='20%'>"+value.cdesc+"</td><td>"
                    +value.gname+"</td><td><img src='"+value.gimg+"' alt='无图片' width=60px height=60px /></td><td>"
                    +value.oprice+"</td><td>"+value.gdesc+"</td><td width='10%'>"
                    +value.detail+"</td><td>"+
                    "<a href=updateCat.html?catId="+value.catid+">修改</a>&nbsp;&nbsp;" +
                    "<a onclick='deleteCat.call(this)' id="+value.catid+">删除</a></td></tr>";
            });
            $tbody.append($tr_array);
            $("#catListTable tbody tr:even").attr("class","success");
            $("#catListTable tbody tr:odd").attr("class","warning");
        },
        error:function () {
            alert("网络错误");
        }
    })
}

function deleteCat() {
    $.ajax({
        url: prefix_path+"deleteCat",
        type:"DELETE",
        dataType:"json",
        contentType:'application/json',
        data:"catId="+$(this).attr("id"),
        success:function (data) {
            if(data.data.msg=="login"){
                alert("请先登录");
                location.href="login.html";
                return;
            }
            alert("删除成功");
        },
        error:function () {
            alert("网络错误");
        }
    });
}